#include "replace.h"
#include "ui_replace.h"
#include <QTextEdit>
#include <QScrollBar>
#include <QMessageBox>

Replace::Replace(QWidget *parent,QTextEdit* _target) :
    QDialog(parent),
    ui(new Ui::Replace),target(_target)
{
    ui->setupUi(this);
    connect(ui->Find,SIGNAL(clicked()),this,SLOT(find()));
    connect(ui->PreBtn, SIGNAL(clicked()), this, SLOT(findPre()));
    connect(ui->NextBtn,SIGNAL(clicked()),this,SLOT(findNext()));
    connect(ui->replaceAll,SIGNAL(clicked()),this,SLOT(replace()));
}

Replace::~Replace()
{
    delete ui;
}

void Replace::findNext(){
    QString str = ui->lineEdit->text();
    if(str.isEmpty())return;
    if (!target->find(str))
    {
       QMessageBox::warning(this, tr("查找"),
                tr("找不到呀😯"));
    }
}

void Replace::findPre(){
    QString str = ui->lineEdit->text();
    if(str.isEmpty())return;
    if (!target->find(str, QTextDocument::FindBackward))
    {
       QMessageBox::warning(this, tr("查找"),
                tr("找不到呀😯"));
    }
}

void Replace::erase(){
    QTextDocument *document = target->document();
    bool found = false;
    QTextCursor highlight_cursor(document);
    QTextCursor cursor(document);
    //开始
    cursor.beginEditBlock();
    QTextCharFormat color_format(highlight_cursor.charFormat());
    color_format.setBackground(Qt::white);  //背景颜色
    while (!highlight_cursor.isNull() && !highlight_cursor.atEnd())
    {
        //查找指定的文本，匹配整个单词
        highlight_cursor = document->find(curr, highlight_cursor);
        if (!highlight_cursor.isNull())
        {
            if (!found)
                found = true;
            highlight_cursor.mergeCharFormat(color_format);
        }
    }
    cursor.endEditBlock();
}

void Replace::find(){
    erase();
    QString search_text = ui->lineEdit->text();
    curr = search_text;
        if (search_text.trimmed().isEmpty())
        {
            return;
        }
        else
        {
            QTextDocument *document = target->document();
            bool found = false;
            QTextCursor highlight_cursor(document);
            QTextCursor cursor(document);
            //开始
            cursor.beginEditBlock();
            QTextCharFormat color_format(highlight_cursor.charFormat());
            color_format.setBackground(Qt::yellow);  //背景颜色
            while (!highlight_cursor.isNull() && !highlight_cursor.atEnd())
            {
                //查找指定的文本，匹配整个单词
                highlight_cursor = document->find(search_text, highlight_cursor);
                if (!highlight_cursor.isNull())
                {
                    if (!found)
                        found = true;
                    highlight_cursor.mergeCharFormat(color_format);
                }
            }
            cursor.endEditBlock();
        }
}

void Replace::replace(){
    double a = target->verticalScrollBar()->value();
    QString temp = target->document()->toPlainText().replace(ui->lineEdit->text(),ui->replace->text());
    target->setPlainText(temp);
    target->verticalScrollBar()->setValue(a);
}

void Replace::replaceAll(){

}
